﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
using System.Data;
using TimKen.BussLogic;
namespace TimKen.Web.Shop
{
    public partial class ShopProJect : TimKen.App.WebPage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentUserName != null && CurrentUserName != "")
                {
                    bindShop();
                }
                else
                {
                    Alert("您必须登录！", "../Index.aspx");
                }   
                if (this.ShopCart.GetItems.Count <= 0)
                {
                    this.chk_edclick.Visible = false;
                }
            }
        }

        public void bindShop()
        {
            var query = from x in ShopCart.GetItems
                        select new
                        {
                            num = x.Value.Count,
                            Money = x.Value.Money,
                            Min = x.Value.Project.Min,
                            MinQuantitative = x.Value.Project.MinQuantitative,
                            Netweight = x.Value.Project.Netweight,
                            ProjectId = x.Value.Project.ProjectId,
                            ProjectXh = x.Value.Project.ProjectXh,
                            Unitprice = x.Value.Project.Unitprice,
                            Date = x.Value.Project.Date,
                            isShow = x.Value.IsShow
                        };
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = query.ToList();
            pds.AllowPaging = true;
            this.AspNetPager1.RecordCount = query.Count();
            pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
            this.AspNetPager1.PageSize = 4;
            pds.PageSize = 4;
            this.gvShopCart.DataSource = pds;
            this.DataBind();
            // ListPagingSize(this.gvShopCart, this.ShopCart, this.AspNetPager1, 5);

        }

        protected void gvShopCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string proid = e.CommandArgument.ToString();
                ShopCart.Remove(proid.Trim());
                bindShop();
            }
        }

        protected void Btn_DelAll_Click(object sender, ImageClickEventArgs e)
        {
            if (ShopCart.GetItems.Count == 0)
            {
                return;
            }
            foreach (GridViewRow dr in this.gvShopCart.Rows)
            {
                CheckBox che = dr.Cells[0].FindControl("chk_Xz") as CheckBox;
                if (che.Checked == true)
                {
                    Button Bt = dr.Cells[1].FindControl("pid") as Button;
                    ShopCart.Remove(Bt.Text.Trim());
                }
            }
            bindShop();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            bindShop();
        }
        ProductManager pm = new ProductManager();
        UserAccountManager uam = new UserAccountManager();
        private bool ProductChecked(ref string printid)
        {
            bool b = true;
            for (int i = 0; i <= gvShopCart.Rows.Count - 1; i++)
            {
                string productId = ((Button)gvShopCart.Rows[i].Cells[1].Controls[1]).Text;
                double sizeNumber = Convert.ToDouble(((Button)gvShopCart.Rows[i].Cells[3].Controls[1]).Text);      //购买数量
                double MinMultiple = Convert.ToDouble(((Button)gvShopCart.Rows[i].Cells[7].Controls[1]).Text);          //最小倍数
                double MinNumber = Convert.ToDouble(((Button)gvShopCart.Rows[i].Cells[8].Controls[1]).Text);    //最小数量
                DateTime datetimeRQ = Convert.ToDateTime(((Button)gvShopCart.Rows[i].Cells[9].Controls[1]).Text);    //货期时间

                int flag = 0;
                if (datetimeRQ == Convert.ToDateTime("2099-12-30") || datetimeRQ == Convert.ToDateTime("2099-12-31"))
                {
                    flag = -3;
                    b = false;

                }
                //if (productId.Length != 8)
                //{
                //    flag = -4;
                //    b = false;
                //}
                //if (!pm.Exists(Convert.ToInt32(productId)))
                //{
                //    flag = -5;
                //    b = false;
                //}
                bool isAuthorizationed = pm.GetProductAuthorization(Convert.ToInt32(productId), uam.GetModel(CurrentUserName).CustomerID.Value);
                if (!isAuthorizationed)
                {
                    printid += productId + ";";
                    b = false;
                    flag = -2;
                    continue;
                }
                //if (MinMultiple * MinNumber == 0 || MinMultiple * MinNumber == 1)
                //{
                //    b = false;
                //    continue;
                //}         //这里的判断还需要梳理
                if (sizeNumber < MinNumber || sizeNumber % MinMultiple != 0)
                {
                    printid += productId + ";";
                    flag = -1;
                    b = false;
                    continue;
                }
                if (flag == 0)
                {
                    b = true;
                    //Response.Redirect("/Order/OrderSend.aspx", true);
                }
                else if (flag == -1)
                {
                    printid += productId + ";";
                    //gvShopCart.Rows[i].BackColor = System.Drawing.Color.Yellow;
                    gvShopCart.Rows[i].Style.Add("text-color", "yellow");
                    Alert("部分产品不符合购买的最小数量和最小倍数!");
                    continue;
                }
                else if (flag == -2)
                {
                    printid += productId + ";";
                    Alert("部分产品未授权购买!");
                    //gvShopCart.Rows[i].BackColor = System.Drawing.Color.Red;
                    gvShopCart.Rows[i].Style.Add("text-color", "red");
                    //e.Row.Style.Add("Color", "red");
                    continue;
                }
                else if (flag == -3)
                {
                    printid += productId + ";";
                    Alert("部分产品货期未确认!");
                    //gvShopCart.Rows[i].BackColor = System.Drawing.Color.Red;
                    gvShopCart.Rows[i].Style.Add("text-color", "red");
                    continue;
                }
                else if (flag == -4)
                {
                    printid += productId + ";";
                    Alert("部分产品编号有问题!");
                    //gvShopCart.Rows[i].BackColor = System.Drawing.Color.Red;
                    gvShopCart.Rows[i].Style.Add("text-color", "red");
                    continue;
                }
                else if (flag == -5)
                {
                    printid += productId + ";";
                    Alert("不存在此类型产品!");
                    gvShopCart.Rows[i].Style.Add("text-color", "red");//.BackColor = System.Drawing.Color.Red;
                    continue;
                }
            }
            return b;
        }

        protected void imgbtn_click_Click(object sender, ImageClickEventArgs e)
        {
            //验证购物车
            //Response.Redirect("CommitOrders.aspx");
            //&&this.ShopCart.GetItems.Count>0
            if (this.ShopCart.IsCommit == true && this.ShopCart.GetItems.Count > 0)
            {
                Response.Redirect("CommitOrders.aspx");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chk_edclick_CheckedChanged(object sender, EventArgs e)
        {
            if (ShopCart.GetItems.Count == 0)
            {
                return;
            }
            if (chk_edclick.Checked)
            {
                var query = from x in ShopCart.GetItems
                            where x.Value.IsShow == false
                            select new
                            {
                                num = x.Value.Count,
                                Money = x.Value.Money,
                                Min = x.Value.Project.Min,
                                MinQuantitative = x.Value.Project.MinQuantitative,
                                Netweight = x.Value.Project.Netweight,
                                ProjectId = x.Value.Project.ProjectId,
                                ProjectXh = x.Value.Project.ProjectXh,
                                Unitprice = x.Value.Project.Unitprice,
                                Date = x.Value.Project.Date,
                                isShow = x.Value.IsShow
                            };
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = query.ToList();
                pds.AllowPaging = true;
                this.AspNetPager1.RecordCount = query.Count();
                pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
                this.AspNetPager1.PageSize = 4;
                pds.PageSize = 4;
                this.gvShopCart.DataSource = pds;
                this.gvShopCart.DataBind();
                // ListPagingSize(this.gvShopCart, this.ShopCart, this.AspNetPager1, 5);
            }
            else
                bindShop();
        }

        protected void gvShopCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Button hid = e.Row.Cells[2].FindControl("isShow") as Button;
            if (hid != null && hid.Text == "False")
            {
                e.Row.ForeColor = System.Drawing.Color.Red;
            }



        }

        protected void gvShopCart_RowCreated(object sender, GridViewRowEventArgs e)
        {
            string s = "";
        }
    }
}